package com.hnkywl.sts.dao.report;

import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.sql.Date;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-15
 * Time: 下午9:13
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class YdcbDao extends SimpleHibernateTemplate {
    /**
     * 票据库存报表应收
     *
     * @param gsid
     * @param
     * @return
     */
    public Map<String, Object> ydcbList(Long gsid, java.util.Date nowDate,String code,String ggxh,String name, Integer pageNo, Integer pageSize) {
        if (gsid == null)
            gsid = -1L;
        if(code==null)
            code = "-1";
        if(ggxh==null)
            ggxh = "-1";
        if(name==null)
            name = "-1";
//        Calendar calendar = GregorianCalendar.getInstance();
//        calendar.setTime(nowDate);
//        calendar.add(Calendar.DATE, 1);
//        java.sql.Date paramDate = new java.sql.Date(nowDate.getTime());
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        Integer total=0;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call RPT_CW_YDCB(?,?,?,?,?,?,?,?)}");
            call.setLong(1, gsid);
//            call.setDate(2, paramDate);
            call.setString(2,code);
            call.setString(3,ggxh);
            call.setString(4,name);
            call.setInt(5, (pageNo - 1) * pageSize);
            call.setInt(6, pageNo * pageSize);
            call.registerOutParameter(7, Types.NUMERIC);
            call.registerOutParameter(8, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            total = call.getInt(7);
            rs = ((OracleCallableStatement) call).getCursor(8);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, Object> result = new HashMap<String, Object>();
            List<Map> rows = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                    rows.add(r);
            }
            result.put("total", total);
            result.put("rows", rows);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }


    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }
}
